<style scoped>
.split {
    height: calc(100vh - 140px);
    background: #ffffff;
    border: 1px solid #dcdee2;
}

.split-pane {
    padding: 10px;
}

/deep/.ivu-table-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}

/deep/.ivu-table-fixed-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}
</style>
<template>
    <div class="split">
        <Split v-model="split1">
            <div slot="left" class="split-pane">
                <Tree :data="treeData" @on-select-change="changeTree"></Tree>
            </div>
            <div slot="right" class="split-pane">
                <Card style="margin-bottom: 10px">
                    <Form inline class="form-search">
                        <FormItem>
                            <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="开始日期" :clearable="true" v-model="sdate"></DatePicker>
                        </FormItem>
                        <FormItem>
                            <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="结束日期" :clearable="true" v-model="edate"></DatePicker>
                        </FormItem>
                        <FormItem>
                            <Input v-model="keyword" placeholder="姓名/医保号" style="width: 200px" />
                        </FormItem>

                        <FormItem>
                            <Button type="primary" @click="page = 1; SearchCustomer();" icon="md-search">查询</Button>
                        </FormItem>
                        <FormItem>
                            <Button type="primary" @click="ExportCustomer();" icon="md-search">导出</Button>
                        </FormItem>
                    </Form>
                </Card>
                <Table ref="table" style="margin-bottom: 10px" border :loading="loadingTable" :columns="tableTitle" :data="tableData" :height="tableHeight"></Table>
                <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100]" @on-change="(s) => { page = s; SearchCustomer(); }" @on-page-size-change="(p) => { size = p; page = 1; SearchCustomer(); }" show-total show-sizer />
            </div>
        </Split>
        <Modal :footer-hide="true" width="800" draggable v-model="modalSaleOrder" :title="customerSaleOrderTitle" :mask-closable="false" sticky>
            <CustomerSaleOrder :Id="tempDataId" v-if="modalSaleOrder" />
        </Modal>
    </div>
</template>

<script>

import { CustomerStatistics, ExportCustomer } from '@/api/customer'
import { GetTreeDepartment } from '@/api/department'
import { InitDepartmentTree } from '../../libs/tools'
import CustomerSaleOrder from './customerSaleOrder.vue'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
    name: 'customer',
    data() {
        return {
            split1: 0.15,
            Extras: {},
            total: 0,
            page: 1,
            size: 10,
            sdate: null,
            edate: null,
            modalData: false,
            modalSaleOrder: false,
            modalPay: false,
            tableHeight: 450,
            keyword: '',
            customerSaleOrderTitle: '',
            ruleValidate: {
                Amount: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ]

            },
            loading: false,
            loadingTable: false,
            tempData: {},
            tableTitle: [
                {
                    key: 'index',
                    type: 'index',
                    title: '序号',
                    align: 'center',
                    width: 50,
                    indexMethod: (params) => {
                        return params._index + (this.page - 1) * this.size + 1;
                    }
                },
                {
                    key: 'DisplayName',
                    title: '姓名',
                    width: 100,
                    align: 'center'

                },
                {
                    key: 'SexText',
                    title: '性别',
                    minWidth: 50,
                    align: 'center'
                },
                // {
                //     key: 'Age',
                //     title: '年龄',
                //     width: 80,
                //     align: 'center'
                // },
                {
                    key: 'DepartmentName',
                    title: '所属部门',
                    width: 150,
                    align: 'center'

                },
                {
                    key: 'Balance',
                    title: '账户余额(元)',
                    width: 100,
                    align: 'center',
                },
                {
                    key: 'MedicalCardNo',
                    title: '医保号',
                    minWidth: 150,
                    align: 'left'
                },
                {
                    key: 'ConsumptionAmount',
                    title: '消费金额(元)',
                    minWidth: 100,
                    align: 'center',
                    render: (h, params) => {
                        return h(
                            'a',
                            {
                                style: {
                                    color: params.row.ConsumptionAmount >= 0 ? '#19be6b' : '#ed4014',
                                    fontSize: '14px',
                                    textDecoration: 'underline'
                                },
                                on: {
                                    click: () => {
                                        this.customerSaleOrderTitle = params.row.Name + '-处方记录'
                                        this.tempDataId = params.row.Id
                                        this.modalSaleOrder = true
                                    }
                                }
                            },
                            params.row.ConsumptionAmount
                        )
                    }
                },
                {
                    key: 'visits',
                    title: '就诊次数',
                    minWidth: 80,
                    align: 'center'
                },
                {
                    key: 'Remark',
                    title: '备注',
                    minWidth: 200,
                    align: 'center'
                },
            ],
            typeid: 0,
            tableData: [],
            treeData: [],
            selectTreeData: [],
            tempDataId: null
        }
    },
    components: {
        CustomerSaleOrder,
    },
    computed: {},

    methods: {
        SearchCustomer() {
            debugger
            let vm = this
            vm.loadingTable = true
            if (vm.sdate != null && vm.sdate != '') {
                if (vm.sdate === 'object') {
                    vm.sdate = vm.dayjs(vm.sdate).format('YYYY年MM月DD日')
                }
            }
            else {
                vm.sdate = null;
            }
            if (vm.edate != null && vm.edate != '') {
                if (vm.edate === 'object') {
                    vm.edate = vm.dayjs(vm.edate).format('YYYY年MM月DD日')
                }
            } else {
                vm.edate = null;
            }
            var reqData = {
                page: vm.page,
                size: vm.size,
                sdate: vm.sdate,
                edate: vm.edate,
                keyword: vm.keyword,
                typeid: vm.typeid
            }
            CustomerStatistics(reqData).then((res) => {
                vm.loadingTable = false
                var data = res.data
                if (data.StatusCode == 200) {
                    vm.tableData = data.Data.Items
                    vm.total = data.Data.TotalCount
                    vm.Extras = data.Extras
                } else {
                    vm.$Notice.error({
                        title: data.Errors
                    })
                }
            })
        },
        ExportCustomer() {
            debugger
            let vm = this
            if (vm.sdate != null && vm.sdate != '') {
                if (vm.sdate === 'object') {
                    vm.sdate = vm.dayjs(vm.sdate).format('YYYY年MM月DD日')
                }
            }
            else {
                vm.sdate = null;
            }
            if (vm.edate != null && vm.edate != '') {
                if (vm.edate === 'object') {
                    vm.edate = vm.dayjs(vm.edate).format('YYYY年MM月DD日')
                }
            } else {
                vm.edate = null;
            }
            var reqData = {
                keyword: vm.keyword,
                typeid: vm.typeid,
                sdate: vm.sdate,
                edate: vm.edate,
            }
            ExportCustomer(reqData).then((res) => {
                this.downloadfile(res)
            }).catch((err) => {
                vm.$Message.error({ title: '导出错误', content: err })
            })
        },
        downloadfile(res) {
            debugger;
            var blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
            var contentDisposition = res.headers['content-disposition']
            var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
            var filename = "人员统计.xlsx"
            if (contentDisposition != undefined) {
                var result = patt.exec(contentDisposition)
                filename = result[1]
            }
            var downloadElement = document.createElement('a')
            var href = window.URL.createObjectURL(blob) // 创建下载的链接
            var reg = /^["](.*)["]$/g
            downloadElement.style.display = 'none'
            downloadElement.href = href
            downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() // 点击下载
            document.body.removeChild(downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL(href)
        },
        GetTreeDepartment() {
            let vm = this
            GetTreeDepartment(0).then((res) => {
                const data = res.data
                if (data.StatusCode == 200) {
                    this.selectTreeData = InitDepartmentTree(data.Data)
                    this.treeData = [
                        {
                            id: 0,
                            label: '全部',
                            title: '全部',
                            value: 0,
                            expand: true,
                            children: this.selectTreeData
                        }

                    ]
                } else {
                    vm.$Message.error({ title: '提示', content: data.Errors })
                }
            })
        },
        changeTree(a, b) {
            this.typeid = b.id
            this.SearchCustomer()
        },
    },
    mounted() {
        this.tableHeight = window.innerHeight - 300
        this.SearchCustomer()
        this.GetTreeDepartment()
    }
}
</script>

<style></style>
